"""
全局Tcl/Tk环境设置模块
确保在任何tkinter导入之前设置环境变量
"""
import sys
import os

def setup_tcltk_environment():
    """
    设置Tcl/Tk环境变量 - 必须在导入tkinter之前调用
    
    这个函数会：
    1. 智能检测多种可能的Tcl/Tk安装路径
    2. 自动设置TCL_LIBRARY和TK_LIBRARY环境变量
    3. 验证init.tcl文件存在
    4. 提供详细的调试信息
    """
    
    if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
        tcl_path = os.path.join(sys._MEIPASS, 'tcl', 'tcl8.6')
        tk_path = os.path.join(sys._MEIPASS, 'tcl', 'tk8.6')
        if os.path.exists(tcl_path):
            os.environ['TCL_LIBRARY'] = tcl_path
            print(f"[OK] Set TCL_LIBRARY: {tcl_path}")
        else:
            print(f"[WARNING] TCL_LIBRARY not set (path not found)")
        if os.path.exists(tk_path):
            os.environ['TK_LIBRARY'] = tk_path
            print(f"[OK] Set TK_LIBRARY: {tk_path}")
        else:
            print(f"[WARNING] TK_LIBRARY not set (path not found)")
        init_tcl_path = os.path.join(tcl_path, 'init.tcl')
        if os.path.exists(init_tcl_path):
            print(f"[OK] Found init.tcl: {init_tcl_path}")
        else:
            print(f"[WARNING] init.tcl not found in: {init_tcl_path}")
        return os.path.exists(tcl_path) and os.path.exists(tk_path)

    # 定义可能的Tcl/Tk安装路径（Windows常见路径）
    possible_paths = [
        # 标准Python安装路径
        os.path.join(sys.prefix, 'tcl', 'tcl8.6'),
        os.path.join(sys.prefix, 'tcl', 'tk8.6'),
        os.path.join(sys.prefix, 'lib', 'tcl8.6'),
        os.path.join(sys.prefix, 'lib', 'tk8.6'),
        
        # Program Files常见路径
        r'C:\Program Files\Python313\tcl\tcl8.6',
        r'C:\Program Files\Python313\tcl\tk8.6',
        r'C:\Program Files\Python312\tcl\tcl8.6',
        r'C:\Program Files\Python312\tcl\tk8.6',
        r'C:\Program Files\Python311\tcl\tcl8.6',
        r'C:\Program Files\Python311\tcl\tk8.6',
        
        # 本地AppData路径
        os.path.join(os.path.expanduser('~'), 'AppData', 'Local', 'Programs', 'Python', 'Python313', 'tcl', 'tcl8.6'),
        os.path.join(os.path.expanduser('~'), 'AppData', 'Local', 'Programs', 'Python', 'Python313', 'tcl', 'tk8.6'),
        
        # Windows系统路径
        r'C:\Windows\System32\tcl\tcl8.6',
        r'C:\Windows\System32\tcl\tk8.6',
    ]
    
    tcl_path = None
    tk_path = None
    
    # 查找tcl路径
    for path in possible_paths:
        if 'tcl8.6' in path and os.path.exists(path):
            tcl_path = path
            break
    
    # 查找tk路径
    for path in possible_paths:
        if 'tk8.6' in path and os.path.exists(path):
            tk_path = path
            break
    
    # 设置环境变量
    if tcl_path and os.path.exists(tcl_path):
        os.environ['TCL_LIBRARY'] = tcl_path
        print(f"[OK] Set TCL_LIBRARY: {tcl_path}")
    else:
        print(f"[WARNING] TCL_LIBRARY not set (path not found)")
    
    if tk_path and os.path.exists(tk_path):
        os.environ['TK_LIBRARY'] = tk_path
        print(f"[OK] Set TK_LIBRARY: {tk_path}")
    else:
        print(f"[WARNING] TK_LIBRARY not set (path not found)")
    
    # 检查init.tcl文件
    if tcl_path:
        init_tcl_path = os.path.join(tcl_path, 'init.tcl')
        if os.path.exists(init_tcl_path):
            print(f"[OK] Found init.tcl: {init_tcl_path}")
        else:
            print(f"[WARNING] init.tcl not found in: {init_tcl_path}")
    
    return tcl_path is not None and tk_path is not None

# 自动调用环境设置
if not os.environ.get('TCL_TK_ENVIRONMENT_SETUP'):
    setup_tcltk_environment()
    os.environ['TCL_TK_ENVIRONMENT_SETUP'] = '1'